package com.gorkr.cronback.pattern.designPattern.observer;

import java.util.HashMap;

public class SpliteCronStringObserver implements CronObserver{

    private CronStringSubject cronStringSubject;

    public SpliteCronStringObserver(CronStringSubject cronStringSubject){
        this.cronStringSubject = cronStringSubject;
        cronStringSubject.registerCronObserver(this);
    }


    @Override
    public Object update(String cronString) throws Exception {
        String[] split = cronString.split("\\s");
        final String[] part = {"second","minute","hour","dayOfMonth","month","dayOfWeek","year",};
        HashMap<String, Object> stringStringHashMap = new HashMap<>();
        int size = split.length;
        if(size==5){
            for (int i = 0; i < 5; i++) {
                stringStringHashMap.put(part[i+1],split[i]);
            }
        }

        return stringStringHashMap;
    }
}
